/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import org.openaecfm.model.structuralload.StructuralLoad;
import org.openaecfm.model.structuralload.StructuralLoadSingleDisplacement;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class StructuralLoadSingleDisplacementIO extends
		AbstractEntityInstanceInputImpl<StructuralLoadSingleDisplacement>
		implements IEntityInstanceUtilInput<StructuralLoadSingleDisplacement>,
		IEntityInstanceOutput<StructuralLoadSingleDisplacement>,
		IEntityInstanceUtilOutput<StructuralLoadSingleDisplacement> {
	private IEntityInstanceUtilInput<StructuralLoad> utilInput;
	private IEntityInstanceUtilOutput<StructuralLoad> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<StructuralLoad> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(StructuralLoadSingleDisplacement pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSTRUCTURALLOADSINGLEDISPLACEMENT",
				appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected StructuralLoadSingleDisplacement doCreateObject() {
		return new StructuralLoadSingleDisplacement();
	}

	public void setUtilInput(IEntityInstanceUtilInput<StructuralLoad> utilInput) {
		this.utilInput = utilInput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(StructuralLoadSingleDisplacement pojo,
			EntityInstance ei, IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DisplacementX
		if (pojo.getDisplacementX() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getDisplacementX()));
		}

		// Set DisplacementY
		if (pojo.getDisplacementY() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getDisplacementY()));
		}

		// Set DisplacementZ
		if (pojo.getDisplacementZ() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getDisplacementZ()));
		}

		// Set RotationalDisplacementRX
		if (pojo.getRotationalDisplacementRX() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo
					.getRotationalDisplacementRX()));
		}

		// Set RotationalDisplacementRY
		if (pojo.getRotationalDisplacementRY() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo
					.getRotationalDisplacementRY()));
		}

		// Set RotationalDisplacementRZ
		if (pojo.getRotationalDisplacementRZ() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo
					.getRotationalDisplacementRZ()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			StructuralLoadSingleDisplacement pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DisplacementX
		Parameter dx = sei.getParameter(1);
		if (dx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) dx;
			pojo.setDisplacementX(r.toDouble());
		}

		// Set DisplacementY
		Parameter dy = sei.getParameter(2);
		if (dy.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) dy;
			pojo.setDisplacementY(r.toDouble());
		}

		// Set DisplacementZ
		Parameter dz = sei.getParameter(3);
		if (dz.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) dz;
			pojo.setDisplacementZ(r.toDouble());
		}

		// Set RotationalDisplacementRX
		Parameter rx = sei.getParameter(4);
		if (rx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) rx;
			pojo.setRotationalDisplacementRX(r.toDouble());
		}

		// Set RotationalDisplacementRY
		Parameter ry = sei.getParameter(5);
		if (ry.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) ry;
			pojo.setRotationalDisplacementRY(r.toDouble());
		}

		// Set RotationalDisplacementRZ
		Parameter rz = sei.getParameter(6);
		if (rz.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) rz;
			pojo.setRotationalDisplacementRZ(r.toDouble());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			StructuralLoadSingleDisplacement pojo, IAppDataManager appDataManager) {
		return;
	}
}
